package src.dynamic_programming;

import java.util.Scanner;

/**
 * @author starsea
 * @date 2024-08-15 20:21
 */

public class Test02 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int V = in.nextInt();
        int[] v = new int[n];
        int[] w = new int[n];
        for (int i = 0; i < n; i++) {
            v[i] = in.nextInt();
            w[i] = in.nextInt();
        }
        System.out.println(ways1(n, V, v, w));
        System.out.println(ways2(n, V, v, w));
    }

    public static int ways1(int n, int V, int[] v, int[] w) {
        //创建dp表
        int[][] dp = new int[n + 1][V + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= V; j++) {
                dp[i][j] = dp[i - 1][j];
                if (j >= v[i - 1]) {
                    dp[i][j] = Math.max(dp[i][j], dp[i][j - v[i - 1]] + w[i - 1]);
                }
            }
        }
        return dp[n][V];

    }

    public static int ways2(int n, int V, int[] v, int[] w) {
        int[][] dp = new int[n + 1][V + 1];
        for (int j = 1; j <= V; j++) {
            dp[0][j] = -1;
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= V; j++) {
                dp[i][j] = dp[i - 1][j];
                if (j >= v[i - 1] && dp[i][j - v[i - 1]] != -1) {
                    dp[i][j] = Math.max(dp[i][j], dp[i][j - v[i - 1]] + w[i - 1]);
                }
            }
        }
        if (dp[n][V] == -1) {
            return 0;
        }
        return dp[n][V];
    }
}
